package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.AppChannelService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.app.Channels;
import com.iot08.common.response.Response;
import com.iot08.common.utils.StringUtil;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @version 1.0
 * @Description:
 * @time: 2020/10/23 17:00
 * @author: Rock
 */
@Controller
@RequestMapping("appChannel")
public class AppChannelController extends BaseController {

    @Resource
    private AppChannelService appChannelService;

    /**
     * @param page
     * @time: 2020/10/23 17:11
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("list")
    public ModelAndView list(PageVO page) {
        ModelAndView mv = new ModelAndView();
        Page<Channels> pages = appChannelService.list(page, this.getSessionUser());
        mv.addObject("channels", pages.getContent());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.setViewName("appChannel/channels_list");
        return mv;
    }

    /**
     * @param
     * @time: 2020/10/23 18:15
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toAdd")
    public ModelAndView toAdd() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("appChannel/channels_add");
        return mv;
    }

    /**
     * @param channel
     * @param request
     * @time: 2020/10/23 18:15
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ModelAndView add(Channels channel) throws IOException {
        ModelAndView mv = new ModelAndView();
        appChannelService.add(channel, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * @param id
     * @time: 2020/10/23 18:18
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toEdit")
    public ModelAndView toEdit(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("channel", appChannelService.getById(id));
        mv.setViewName("appChannel/channels_edit");
        return mv;
    }

    /**
     * @param channel
     * @time: 2020/10/23 18:36
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public ModelAndView edit(Channels channel) {
        ModelAndView mv = new ModelAndView();
        appChannelService.update(channel, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * @param id
     * @time: 2020/10/23 18:39
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping(value = "del", method = RequestMethod.POST)
    public Response<Object> del(String id) {
        return appChannelService.del(id, this.getSessionUser(), this.getRemortIP());
    }

    /**
     * @param id
     * @param shortName
     * @time: 2020/11/2 11:54
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping("checkShortName")
    public Response<Object> checkShortName(@RequestParam(required = false) String id, String shortName) {
        Channels channel = appChannelService.getByShortName(shortName);
        if (StringUtil.isBlank(id)) {
            if (channel != null) {
                return Response.failedResponse("APP渠道标识已存在");
            }
        } else {
            if (channel != null && !id.equals(channel.getId())) {
                return Response.failedResponse("APP渠道标识已存在");
            }
        }
        return Response.successResponse();
    }
}
